package view
{
	import laya.display.Sprite;
	import laya.display.Animation;
	import laya.maths.Point;

	/**
	 * ...
	 * @author
	 */
	public class Enemy extends Sprite{
		private var roleAni:Animation;
		private var type:int;
		private var speed:int;

		public var hitRadius:Point;
		public var hp:int;
		public var attack:int;
		public var isDie:Boolean;
		public var score:int;
		public function Enemy(){
			hitRadius = new Point();
			roleAni=new Animation();
			roleAni.loadAnimation("Role.ani");

			this.addChild(roleAni);
			GameDispatcher.getInstance().on(EventName.GAME_PAUSE,this,onPause);
			GameDispatcher.getInstance().on(EventName.GAME_RESUME,this,onResume);
		}

		public function init(x:int,y:int,type:int,speed:int,hp:int,attack:int,score:int):void
		{
			this.type = type;
			switch(this.type)
			{
				case 1:
					hitRadius.x = 57;
					hitRadius.y = 51;
					break;
				case 2:
					hitRadius.x = 69;
					hitRadius.y = 95;
					break;
				case 3:
					hitRadius.x = 165;
					hitRadius.y = 260;
					break;
			}
			this.speed = speed;
			this.attack = attack;
			this.score = score;
			this.hp = hp;
			this.x = x;
			this.y = y;
			isDie = false;
			LayerManager.roleLayer.addChild(this);
			startMove();
			playAction("fly");
		}

		private function playAction(name:String, isLoop:Boolean = true):void
		{
			roleAni.play(0,isLoop,"enemy" + this.type + "_" + name);
		}

		private function startMove():void
		{
			Laya.timer.frameLoop(1,this,moveDown);
		}

		private function moveDown():void
		{
			this.y += this.speed;
			if(this.y > 1280)
			{
				Laya.timer.clear(this,moveDown);
				this.removeSelf();
			}
		}

		public function hit(num:int):void
		{
			hp -= num;
			if(hp <= 0)
			{
				die();
			}
		}

		public function die():void
		{
			isDie = true;
			playAction("die",false);
			GameDispatcher.getInstance().event(EventName.ADD_SCORE,this.score);
			Laya.timer.frameOnce(8,this,clear);
		}

		private function clear():void
		{
			Laya.timer.clear(this,moveDown);
			this.removeSelf();
		}

		private function onPause():void
		{
			Laya.timer.clear(this,moveDown);
		}

		private function onResume():void
		{
			startMove();
		}
	}

}